#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>

void helper_manifest_create_list(char *indata, size_t insize, char *datatype, int dt_argc, char **dt_args, char **data, size_t *size) {
	int i;
	char *ptr;


	(*size) = sizeof(uint16_t);
	(*size) += strlen(datatype) + 1;
	for (i = 0; i < dt_argc; i++) {
		(*size) += strlen(dt_args[i]) + 1;
	}
	(*size) += insize;

	(*data) = malloc(*size);
	ptr = (*data);

	strcpy(ptr, datatype);
	ptr += strlen(datatype) + 1;

	*(uint16_t*)(ptr) = (uint16_t)dt_argc;
	ptr += sizeof(uint16_t);
	for (i = 0; i < dt_argc; i++) {
		strcpy(ptr, dt_args[i]);
		ptr += strlen(dt_args[i]) + 1;
	}
	memcpy(ptr, indata, insize);
}

void helper_manifest_get_payload(char *indata, size_t insize, char **data, size_t *size) {
	int i;
	uint16_t argc;

	(*data) = indata;
	(*size) = insize;

	argc = *(uint16_t*)indata;
	
	(*size) -= sizeof(uint16_t);
	(*data) = &indata[insize - (*size)];

	(*size) -= strlen(*data) + 1;
	(*data) = &indata[insize - (*size)];

	for (i = 0; i < argc; i++) {
		(*size) -= strlen(*data) + 1;
		(*data) = &indata[insize - (*size)];
	}
}
